<?php
namespace app\platform\controller\admin;
use app\BaseController;
use app\beautiful\model\finance\AccountBalanceModel;
use app\beautiful\model\finance\WithdrawAccountLogModel;
use app\beautiful\model\finance\WithdrawModel;
use app\common\model\ShopBalanceLogsModel;
use app\stores\model\promotion\PromotionmoneydetailModel;
use app\stores\model\promotion\PromotionrecordModel;
use app\stores\model\withdraw\MemberWithdrawModel;
use think\facade\Db;


class Withdraw extends BaseController{
    /*商家提现*/
    public function shopWithdrawList()
    {
        $status = $this->request->param('status/d',0);//0待处理1已完成
        $name = $this->request->param('name');
        $start_time = $this->request->param('start_time');
        $end_time = $this->request->param('end_time');
        $limit=$this->request->limit;
        $page=$this->request->page;

        if($start_time && $end_time && $status==1){
            $where['start_time'] = $start_time;
            $where['end_time'] = $end_time;
        }

        if($status==1){
            $status = [-1,1];
        }else{
            $status = [0,2];
        }

        $order=['add_time'=>'desc'];
        $where['status'] = $status;
        $where['name'] = $name;
        $return = WithdrawModel::list($where,$order,$page,$limit);

        $total_money = WithdrawModel::where(['is_delete'=>0])->whereIn('status',[0,2])->sum('money');
        $return['total_money'] = sprintf("%.2f",$total_money);
        $return['total_num'] = WithdrawModel::where(['is_delete'=>0])->whereIn('status',[0,2])->count();
        //已完成的提现笔数，提现金额，提现失败的金额
        $wc_total_money = WithdrawModel::where(['status'=>1,'is_delete'=>0])->sum('money');
        $wc_total_num = WithdrawModel::where(['status'=>1,'is_delete'=>0])->count();
        $fail_total_money = WithdrawModel::where(['status'=>-1,'is_delete'=>0])->sum('money');
        $return['wc_total_money'] = sprintf("%.2f",$wc_total_money);
        $return['wc_total_num'] = $wc_total_num;
        $return['fail_total_money'] = sprintf("%.2f",$fail_total_money);
        return $this->success($return);
    }
    public function shopWithdrawStatus()
    {
        $status = $this->request->param('status/d',0);
        $withdraw_id = $this->request->param('withdraw_id/d',0);

        if(!in_array($status,[-1,2])) return $this->error('状态有误');
        $withdraw = WithdrawModel::getById(['withdraw_id'=>$withdraw_id]);
        if(!$withdraw) return $this->error('提现不存在');
        if($withdraw['status']!=0) return $this->error('该提现已处理');
        Db::startTrans();
        try{
            //拒绝则返回账户
            if($status<0){
                $account = AccountBalanceModel::getById(['shop_id'=>$withdraw['shop_id'],'is_delete'=>0]);
                $log = new WithdrawAccountLogModel();
                $data['shop_id'] = $withdraw['shop_id'];
                $data['account_balance_id'] = $account['account_balance_id'];
                $data['type'] = 1;
                $data['info'] = '提现失败退回';
                $data['type_id'] = 1;
                $data['balance'] = $withdraw['money'];
                $data['withdraw_id'] = $withdraw_id;
                $data['log_type'] = $withdraw['type'];
                $log->save($data);
                if($withdraw['type']==1){
                    $inc = 'account_money';
                }else{
                    $inc = 'lock_money';
                }
                AccountBalanceModel::where(['shop_id'=>$withdraw['shop_id'],'is_delete'=>0])->inc($inc,$withdraw['money'])->update();

            }
            $update['status'] = $status;
            $update['operate_id'] = $this->request->adminId;;
            if($status==2) $update['pass_time'] = date('Y-m-d H:i:s');

            WithdrawModel::update($update,['withdraw_id'=>$withdraw_id]);

            Db::commit();
        } catch (\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());
        }

        return $this->success('操作成功');
    }

    public function shopWithdrawPayment()
    {
        $withdraw_id = $this->request->param('withdraw_id/d',0);
        $pay_type = $this->request->param('pay_type/d',1);
        $pay_voucher = $this->request->param('pay_voucher');
        $remarks = $this->request->param('remarks');

        if(!in_array($pay_type,[1,2,3])) return $this->error('打款方式有误');

        $withdraw = WithdrawModel::getById(['withdraw_id'=>$withdraw_id]);
        if(!$withdraw) return $this->error('提现不存在');
        if($withdraw['status']!=2) return $this->error('该提现不能打款');
        Db::startTrans();
        try{
            WithdrawModel::update(['status'=>1,'pay_type'=>$pay_type,'pay_voucher'=>$pay_voucher,'remarks'=>$remarks,'pay_time'=>date('Y-m-d H:i:s')],['withdraw_id'=>$withdraw_id]);
            //修改账户明细
            ShopBalanceLogsModel::up(['status'=>1,'edit_time'=>date('Y-m-d H:i:s')],['withdraw_id'=>$withdraw_id]);

            Db::commit();

            return $this->success('操作成功');

        } catch (\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());
        }

    }
    /*用户提现*/
    public function memberWithdrawList()
    {
        $status = $this->request->param('status/d',0);//0待处理1已完成
        $name = $this->request->param('name');
        $limit=$this->request->limit;
        $page=$this->request->page;
        if($status==1){
            $status = [-1,1];
        }else{
            $status = [0,2];
        }
        $order=$status==1?['pay_time'=>'desc']:['add_time'=>'desc'];
        $where['status'] = $status;
        $where['name'] = $name;
        $return = MemberWithdrawModel::list($where,$order,$page,$limit);

        $total_money = MemberWithdrawModel::where(['is_delete'=>0])->whereIn('status',[0,2])->sum('money');
        $return['total_money'] = sprintf("%.2f",$total_money);
        $return['total_num'] = MemberWithdrawModel::where(['is_delete'=>0])->whereIn('status',[0,2])->count();
        return $this->success($return);
    }
    /*同意提现*/
    public function memberWithdrawStatus()
    {
        $status = $this->request->param('status/d',0);
        $withdraw_id = $this->request->param('withdraw_id/d',0);

        if(!in_array($status,[-1,2])) return $this->error('状态有误');
        $withdraw = MemberWithdrawModel::info(['withdraw_id'=>$withdraw_id]);
        if(!$withdraw) return $this->error('提现不存在');
        if($withdraw['status']!=0) return $this->error('该提现已处理');
        Db::startTrans();
        try{
            //拒绝则返回账户
            if($status<0){
                $p_r = PromotionrecordModel::info(['member_id'=>$withdraw['member_id'],'team_id'=>$withdraw['team_id']]);
                PromotionrecordModel::up(['money'=>$p_r['money'] + $withdraw['money']],['id'=>$p_r['id']]);

                $p_m_d = new PromotionmoneydetailModel();
                $p_m_d->member_id = $withdraw['member_id'];
                $p_m_d->team_id = $withdraw['team_id'];
                $p_m_d->withdraw_id = $withdraw_id;
                $p_m_d->type = 1;
                $p_m_d->status = 1;
                $p_m_d->money = $withdraw['money'];
                $p_m_d->old_money = $p_r['money'];
                $p_m_d->new_money = $p_r['money'] + $withdraw['money'];
                $p_m_d->info = '提现失败退回';
                $p_m_d->save();

            }
            $update['status'] = $status;
            $update['operate_id'] = $this->request->adminId;;
            if($status==2) $update['pass_time'] = date('Y-m-d H:i:s');

            MemberWithdrawModel::up($update,['withdraw_id'=>$withdraw_id]);

            Db::commit();
        } catch (\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());
        }

        return $this->success('操作成功');
    }
    /*打款*/
    public function memberWithdrawPayment()
    {
        $withdraw_id = $this->request->param('withdraw_id/d',0);
        $pay_type = $this->request->param('pay_type/d',1);
        $pay_voucher = $this->request->param('pay_voucher');
        $remarks = $this->request->param('remarks');

        if(!in_array($pay_type,[1,2,3])) return $this->error('打款方式有误');

        $withdraw = MemberWithdrawModel::info(['withdraw_id'=>$withdraw_id]);
        if(!$withdraw) return $this->error('提现不存在');
        if($withdraw['status']!=2) return $this->error('该提现不能打款');

        MemberWithdrawModel::up(['status'=>1,'pay_type'=>$pay_type,'pay_voucher'=>$pay_voucher,'remarks'=>$remarks,'pay_time'=>date('Y-m-d H:i:s')],['withdraw_id'=>$withdraw_id]);
        return $this->success('操作成功');
    }
}
